package com.umsoft.chapter4.test;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLibDynamicProxy implements MethodInterceptor {
    private static CGLibDynamicProxy cgLibDynamicProxy = new CGLibDynamicProxy();

    private CGLibDynamicProxy(){}

    public static CGLibDynamicProxy getInstance(){
        return cgLibDynamicProxy;
    }
    public <T> T getProxy(Class<T> cls){
        return (T) Enhancer.create(cls,this);
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        doBefore();
        Object result = methodProxy.invokeSuper(obj,objects);
        doAfter();
        return result;
    }


    private void doAfter() {
        System.out.println("After……");
    }

    private void doBefore() {
        System.out.println("Before……");

    }
}
